<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'

const animationDuration = 3000

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        radar: {
          radius: '66%',
          center: ['50%', '42%'],
          splitNumber: 8,
          splitArea: {
            areaStyle: {
              color: 'rgba(127,95,132,.3)',
              opacity: 1,
              shadowBlur: 45,
              shadowColor: 'rgba(0,0,0,.5)',
              shadowOffsetX: 0,
              shadowOffsetY: 15
            }
          },
          indicator: [
            { name: '检测准确率', max: 100 },
            { name: '检测速度', max: 100 },
            { name: '误报率', max: 10 },
            { name: '漏报率', max: 10 },
            { name: '召回率', max: 100 },
            { name: 'mAP50', max: 100 }

          ]
        },
        legend: {
          left: 'center',
          bottom: '10',
          data: ['早熟禾', '藜（灰菜）', '刺儿菜', '莎草', '马齿苋', '玉米幼苗']
        },
        series: [{
          type: 'radar',
          symbolSize: 0,
          areaStyle: {
            normal: {
              shadowBlur: 13,
              shadowColor: 'rgba(0,0,0,.2)',
              shadowOffsetX: 0,
              shadowOffsetY: 10,
              opacity: 1
            }
          },
          data: [
            {
              value: [88, 90, 6, 5, 80, 77],
              name: '早熟禾'
            },
            {
              value: [96, 95, 1, 3, 88, 96],
              name: '藜（灰菜）'
            },
            {
              value: [93, 85, 2, 4, 83, 95],
              name: '刺儿菜'
            },
            {
              value: [95, 88, 3, 6, 79, 94],
              name: '莎草'
            },
            {
              value: [97, 92, 2, 5, 88, 97],
              name: '马齿苋'
            },
            {
              value: [91, 91, 3, 6, 84, 90],
              name: '玉米幼苗'
            }
          ],
          animationDuration: animationDuration
        }]
      })
    }
  }
}
</script>
